package diona.components

import diona.search.PostInfo
import net.liftweb.util.CssSel
import net.liftweb.http.Templates
import net.liftweb.util.BindHelpers._
import org.apache.commons.lang.StringEscapeUtils
import xml.{XML, NodeSeq}

/**
 * User: anton
 * Date: 22.07.11 20:58
 */
class PostDetailsComponent(val post: PostInfo)
{
  def render(): NodeSeq =
  {
    val template: NodeSeq = Templates(List("templates-hidden", "postInfo")).open_!
    bindCss(template)
  }

  private lazy val bindCss: CssSel = {
    ".postItemText *" #> PostDetailsComponent.textAsHtml(post)
  }
}

object PostDetailsComponent
{
  def apply(post: PostInfo): NodeSeq = new PostDetailsComponent(post).render()

  def textAsHtml(post: PostInfo): NodeSeq =
  {
    val xml = "<span>" + StringEscapeUtils.unescapeHtml(post.snippet.getOrElse("")
      .replaceAll("&", "&amp;")) + "</span>"
    XML.loadString(xml)
  }
}